package domain;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "search_result")
public class SearchResultList extends PersistentEntity {
	@OneToOne(fetch=FetchType.EAGER)
	private User user;
	@Column(length=50)
	private String name;
	private Date date;
	@Column(length=50)
	private String description;

	@ManyToMany(cascade=CascadeType.ALL)
	private List<SearchResultItem> results;

	//hibernate
	public SearchResultList() {
		this.results = new ArrayList<SearchResultItem>();
	}
	
	public SearchResultList(User user, String name, String description) {
		this.setUser(user);
		this.setName(name);
		this.setDate(new Timestamp(Calendar.getInstance().getTime().getTime()));
		this.setDescription(description);
		this.results = new ArrayList<SearchResultItem>();
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}
	
	public List<SearchResultItem> getResults() {
		Collections.sort(results);
		return results;
	}
	
	public void addResults(List<User> users) {
		for (User u: users) {
			if (!this.results.contains(new SearchResultItem(u))) {
				this.results.add(new SearchResultItem(u));
			}
		}
	}

	public void removeResults(List<User> users) {
		for (User u: users) {
			this.results.remove(new SearchResultItem(u)); 
		}
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return date;
	}	

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}
}